package com.mai4j.maiadmin.setup.gen;

import com.mai4j.maiadmin.setup.Env;
import lombok.extern.slf4j.Slf4j;
import org.jooq.codegen.GenerationTool;
import org.jooq.meta.jaxb.Configuration;
import org.jooq.meta.jaxb.Jdbc;
import org.jooq.meta.jaxb.Strategy;
import picocli.CommandLine;

import java.util.concurrent.Callable;

/**
 * Created by 席有芳 on 2021/11/22.
 *
 * @author 席有芳
 */
@CommandLine.Command(name = "jooq", description = "jOOQ代码生成")
@Slf4j
public class GenJooq implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        Env.Db db = Env.db();
        Configuration configuration = new Configuration()
                .withJdbc(new Jdbc()
                        .withUrl(db.getUrl())
                        .withUser(db.getUsername())
                        .withPassword(db.getPassword()))
                .withGenerator(new org.jooq.meta.jaxb.Generator()
                        .withGenerate(new org.jooq.meta.jaxb.Generate()
                                .withDaos(true)
                                .withSpringAnnotations(true)
                        )
                        .withStrategy(new Strategy()
                                .withName(getClass().getPackage().getName() + ".jooq.SkipPrefixGeneratorStrategy"))
                        .withDatabase(new org.jooq.meta.jaxb.Database()
                                .withName("org.jooq.meta.mysql.MySQLDatabase")
                                .withIncludes(".*")
                                .withExcludes("")
                                .withInputSchema(db.getDatabase())
                                .withOutputSchemaToDefault(Boolean.TRUE)
                        )
                        .withTarget(new org.jooq.meta.jaxb.Target()
                                .withPackageName("com.mai4j.maiadmin.generated")
                                .withDirectory("../jooq-generated/src/main/java")));
        GenerationTool.generate(configuration);
        return 0;
    }
}
